<?php

namespace App\Library\Service;

use App\Library\Logic\CouponLogic;
use App\Library\Logic\CouponUserRelatedLogic;
use App\Library\Requests\Base\SerialNumberRequest;
use App\Library\Requests\Coupon\CouponPageRequest;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\DB;

class CouponService extends BaseService
{
    public function __construct(CouponLogic $logic)
    {
        $this->logic = $logic;
    }

    /**
     * 优惠券领取列表
     * @param CouponPageRequest $request
     * @return JsonResponse
     */
    public function paging(CouponPageRequest $request): JsonResponse
    {
        return success($this->logic->paging($request));
    }

    /**
     * 领取更多优惠券列表
     * @param CouponPageRequest $request
     * @return JsonResponse
     */
    public function claimedPaging(CouponPageRequest $request): JsonResponse
    {
        return success($this->logic->claimedPaging($request));
    }

    /**
     * 领取优惠券
     * @param SerialNumberRequest $request
     * @return JsonResponse
     *
     */
    public function claimCoupon(SerialNumberRequest $request): JsonResponse
    {
        $coupon = $this->logic->findBySerialNumber($request);
        if (is_null($coupon)) return fail(433);
        //else if ($coupon->system) return fail(621);
        else if ($coupon->status !== 1) return fail(621);
        else if (CouponUserRelatedLogic::getCount($coupon->id) >= $coupon->get_count) {
            $coupon->send_complete_at = now()->toDateTimeString();
            $coupon->save();
            return fail(623);
        };
//        if ($coupon->send_way === 1 && $coupon->point > PointLogic::myPoint()) return failMessage('积分不足');

        DB::beginTransaction();
        try {
            CouponUserRelatedLogic::add($coupon->id);
            $coupon->receive_count++;
            if ($coupon->quantity > 0 && $coupon->receive_count === $coupon->quantity) $coupon->status = 4;
            DB::commit();
            $coupon->save();
            return success();
        } catch (Exception $e) {
            DB::rollBack();
            return fail(622);
        }
    }
    /**
     * 我的优惠券列表
     * @param CouponPageRequest $request
     * @return JsonResponse
     */
    public function myPaging(CouponPageRequest $request): JsonResponse
    {
        return success(CouponUserRelatedLogic::myPaging($request));
    }

    /**
     * 领取优惠券
     * @param SerialNumberRequest $request
     * @return JsonResponse
     */
    public function receive(SerialNumberRequest $request): JsonResponse
    {
        $coupon = $this->logic->findBySerialNumber($request);
        if (is_null($coupon) || $coupon->status !== 1 || $coupon->is_system == 1) {
            return failMessage('优惠券不存在或已领完');
        } else if (!is_null($coupon->end_at) && now()->gte(now()->parse($coupon->end_at))) {
            return failMessage('优惠券已过期');
        } else if (CouponUserRelatedLogic::receiveCount($coupon->id) === $coupon->get_count) {
            return failMessage('用户最多领取' . $coupon->get_count . '张');
        }
        CouponUserRelatedLogic::insertForModel(['couponId' => $coupon->id, 'userId' => USERID]);
        $coupon->receive_count++;
        if ($coupon->quantity > 0 && $coupon->receive_count === $coupon->quantity) {
            $coupon->status = 3;
            $coupon->send_complete_at = now();
        }
        $coupon->save();
        return success();
    }

    /**
     * 未使用优惠券数量
     * @return JsonResponse
     */
    public function noUseCount(): JsonResponse
    {
        return success(CouponUserRelatedLogic::noUseCount());
    }
}
